<%@page import="java.util.Iterator"%>
<%@page import="java.util.Collections"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.google.appengine.api.memcache.MemcacheServiceFactory"%>
<%@page import="com.google.appengine.api.memcache.MemcacheService"%>
<%@page import="com.google.appengine.api.users.User"%>
<%@page import="com.google.appengine.api.users.UserServiceFactory"%>
<%@page import="com.google.appengine.api.users.UserService"%>
<%@page import="java.util.Date"%>
<%@page import="com.tasks.*"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Cloud Tasks</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <link href="../../bootstrap/css/bootstrap.css" rel="stylesheet">
    <link href="../../bootstrap/css/bootstrap-responsive.css" rel="stylesheet">

    <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
  </head>
<body onload="document.taskform.task.focus();">
<div class="container-fluid">

<%

	UserService userService = UserServiceFactory.getUserService();
	User user = userService.getCurrentUser();
	
	if (user == null) {
	    response.sendRedirect(userService.createLoginURL(request.getRequestURI()));
	    return;
	}

%>
<header class="jumbotron subhead" id="overview">
  <h1>Cloud Tasks using Google Cloud Endpoints</h1>
  <p class="lead">Tasks for <%=user.getEmail()%></p>
</header>  

<form name="taskform" method="post" action="newtasks.jsp" class="well form-inline">
  <input type="text" name="task" class="span3" placeholder="New Task">
  <button type="submit" class="btn btn-primary">Create</button>
</form>

<%
		//Caching to MemCache
	    MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
		List<Task> tasks = (List<Task>) syncCache.get(user.getEmail());
    
    
		TaskService ts = new TaskService();
		
		if (tasks == null) {
			tasks = ts.listTask();
			syncCache.put(user.getEmail(), new ArrayList(tasks));
		}
		
		//Add task
		String task = request.getParameter("task");
		if (task != null && task.length() > 0) {
			Task t = new Task();
			t.setNote(task);
			t.setDueDate(new Date().toString());
			t.setEmailAddress(user.getEmail());
			
			ts.insertTask(t);
			tasks.add(t);
			syncCache.put(user.getEmail(), new ArrayList(tasks));
		}
		
		//Delete task
		String id = request.getParameter("id");
		if (id != null && id.length() > 0) {
			ts.removeTask(Long.parseLong(id));
			Iterator itr = tasks.iterator();
			while (itr.hasNext()) {
				Task t = (Task) itr.next();
				if (t.getId().toString().equalsIgnoreCase(id)) {
					itr.remove();
				}
			}
			syncCache.put(user.getEmail(), new ArrayList(tasks));
		}
		
		//List tasks
		if (ts != null && tasks != null && tasks.size() > 0) {
			out.println ("<table class=\"table table-striped table-bordered table-condensed\" border=\"1\" align=\"center\">");
			out.println ("<thead><tr><th>Task</th><th>Due Date</th><th></th></tr></thead><tbody>");
			Collections.reverse(tasks);
			for (Task t:tasks) {
				out.println("<tr>");
				out.println("<td>" + t.getNote() + "</td>"
						+ "<td>"+ t.getDueDate() + "</td>"
						);
				out.println("<td><a class=\"btn btn-danger\" href=\"/newtasks.jsp?id="
						+ t.getId() + "\">" + 
						"<i class=\"icon-trash icon-white\"></i> </a></td>");
						
				out.println("</tr>");
			}
			out.println ("</tbody></table>");
		}		

%>

</div>
</body>


</html>